package com.xiaoshuidi.cloud.module.report.dal.mapper.report;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.xiaoshuidi.cloud.framework.mybatis.core.mapper.BaseMapperX;
import com.xiaoshuidi.cloud.module.report.controller.finance.vo.DetailIncomePageReqVO;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.IncomeDetailReportDO;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.IncomeSummaryReportDO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

@Mapper
@InterceptorIgnore(tenantLine = "true")
public interface IncomeDetailReportMapper extends BaseMapperX<IncomeDetailReportDO> {

    @Select("select count(id)\n" +
             " from report_income_detail\n" +
            " where statistics_month = #{currentMonth}")
    long countDetailsInCurrentMonth(@Param("currentMonth")String currentMonth);

    long countListForPage(@Param("req") DetailIncomePageReqVO reqVO);

    List<IncomeDetailReportDO> selectListForPage(@Param("req") DetailIncomePageReqVO reqVO);

    List<IncomeDetailReportDO> selectListForExport(@Param("req") DetailIncomePageReqVO reqVO);

    List<IncomeDetailReportDO> selectListByContractIdAndStatisMonth(@Param("month") String month,@Param("roomId") Long roomId);

    @Select("select * " +
            " from report_income_detail " +
            " where contract_id = #{contractId} and statistics_month = #{currentMonth} limit 1")
    IncomeDetailReportDO selectDetailsByContractIdInCurrentMonth(@Param("contractId")Long contractId, @Param("currentMonth")String currentMonth);


    @Select("select sum(cur_rent_reduce+cur_other_reduce) amount " +
            " from report_income_detail " +
            " where contract_id = #{contractId} ")
    BigDecimal sumTotalRentAndWaterByContractId(@Param("contractId")Long contractId);

    @Select("select MAX(is_concentrated) isConcentrated,MAX(apartment_id) apartmentId,MAX(apartment_name) apartmentName,MAX(district) district,MAX(city) city, MAX(statistics_month) statisticsMonth," +
            " sum(cur_rent_reduce) rentIn,sum(cur_other_reduce) otherIn,sum(cur_rent_reduce+cur_other_reduce) sumIn, sum(cold_water_fee) sumColdWaterFee," +
            "sum(hot_water_fee) sumHotWaterFee,sum(electric_fee) sumElectricFee,sum(property_fee) sumPropertyFee,sum(maintenance_fee) sumMaintenanceFee," +
            "sum(clean_fee) sumCleanFee,sum(house_transfer_fee) sumHouseTransferFee,sum(liquidated_fee) sumLiquidatedFee,sum(delay_fee) sumDelayFee, " +
            "sum(damages) sumDamages, sum(late_fee) sumLateFee,MAX(tenant_id) tenantId," +
            " MAX(dept_id) deptId from report_income_detail where statistics_month = #{currentMonth} GROUP BY apartment_id")
    List<IncomeSummaryReportDO> selectDetailsInCurrentMonthGroupByApartmentId(@Param("currentMonth")String currentMonth);


}
